package uno.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public abstract class Card extends JPanel implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6719417273029970800L;
	private int width;
	private int height;
	private CardValue value;
	private HandOfCard parent;
	
	
	public Card(int width, int height, CardValue value, HandOfCard parent) {
		this.parent = parent;
		this.value = value;
		this.width = width;
		this.height = height;
		this.setVisible(true);
	    this.setBorder(BorderFactory.createLineBorder(Color.black));
		this.repaint();
		addCardMovesListeners();
	}
	
	/**
	 * Funziona solo quando la carta e' 
	 * 
	 * @return the parent
	 */
	public HandOfCard getParent() {
		return parent;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(HandOfCard parent) {
		this.parent = parent;
	}	
	
	/**
	 * @return the width
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * @param width the width to set
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * @param height the height to set
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * */
	public Dimension getPreferredSize() {
		return new Dimension(getWidth(), getHeight());
	}
	
	/**
	 * @return the value
	 */
	public CardValue getValue() {
		return value;
	}

	/* disegna l'immagine sul canvas a partire dall'origine */
	public void paint(Graphics g) {
		super.paint(g);
		//da qui dico quanto devono essere grandi le immagini 
		g.drawImage(value.getCardImage(), 0, 0, getWidth(), getHeight(), this);
	}
	
	@Override
	public String toString() {
		return "Card, value: " + value; 
	}
	
	/**
	 * toglie o aggiunge il click listener alla carta
	 * */
	public boolean enableClickCardListener(boolean flag) {
		
		boolean result = false;
		
		// System.out.println("1) Numero di listener associati alla carta " + getMouseListeners().length);
		
		if(!flag && getMouseListeners().length > 1) {
			
			removeMouseListener(getMouseListeners()[1]);
			result = true;
			
		} else if(!flag && getMouseListeners().length <= 1) { 
			
			System.err.println("WARNING: la carta non ha associato alcun click listener. Impossibile rimuoverne uno.");
			
		} else if(flag && getMouseListeners().length <= 1) {
			
			addCardClickListeners();
			result = true;
		//	System.out.println("Aggiungo il click listener all'oggetto " + this);
		
		} else if(flag && getMouseListeners().length > 1) {
		
			System.err.println("WARNING: la carta ha gia' associato un click listener. Non ne verra' aggiunto alcuno.");
		
		} else {
			
			System.err.println("WARNING: possono esserci verificati degli errori nella rimozione o aggiunta di\n\t listener alle carte della mano");
			
		}
		
	//	System.out.println("2) Numero di listener associati alla carta " + getMouseListeners().length);

		return result;
	}
	
	protected abstract void addCardClickListeners();
	
	/**
	 * Aggiunge i listener all'oggetto
	 * */
	protected void addCardMovesListeners() {
		addMouseListener(new MouseAdapter(){
			@Override
			public void mouseEntered(MouseEvent e) {
				super.mouseEntered(e);
				//ingrandisco la carta del 10%
				int widthTMP = getWidth() + (int)(((float)getWidth()/100) * 10);
				int heightTMP = getHeight() + ((getHeight()/100) * 10);
				setWidth(widthTMP);
				setHeight(heightTMP);
				setSize(getWidth(), getHeight());
				repaint();
			}

			@Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				//ripristino la carta alle sue dimensioni originali
				int widthTMP = getWidth() - ((getWidth()/100) * 10);
				int heightTMP = getHeight() - ((getHeight()/100) * 10);
				setWidth(widthTMP);
				setHeight(heightTMP);
				setSize(getWidth(), getHeight());
				repaint();
			}
		});
		
	}
	
}
